#This program allows to insert and search a contacit
.section .rodata
optMsg: .asciz"1-Insert new contact\n2-Search contact \n"
error: .asciz" An error has ocurred when it tries to open the file: \n"

asktel: .asciz"Please enter the contact's telephone: \n"
askname: .asciz" Please enter the contact's name: \n"
askemail: .asciz" Please enter the contact's email: \n"

file: .asciz"agenda.txt"
Bl: .asciz"B"
Cl: .asciz"C"
cad4: .asciz "%d"
msg: .asciz "The highest value is: %c\n"
formatInteger: .asciz "%d"
formatString: .asciz "%s"
formatSeparator:.asciz "%s | %d | %s\n"


mode1: .asciz "a" #writing only mode, create a new file if it is not created yet. the pointer is in the end
mode2: .asciz "r" #read only mode.  The pointer is in the top

.section .bss
name: .space 40
tel: .space 4
email: .space 40
option: .space 4
fp: .space 4 #file pointer
.section .text
.globl _start
_start:
      pushl $optMsg
      call printf
      addl $4, %esp
      pushl $option  #read the option chosen
      pushl $formatInteger #the format is inserted. this is important at the moment of reading something through console
      call scanf #read the value
      addl $4, %esp #clean the stack
     call readChoice #call the function that process the option chosen
 
#end program
      movl $1, %eax
      movl $0, %ebx
      int $0x80

readChoice:          pushl %ebp        #prologue
                     movl %esp, %ebp   #prologue
                     cmpl $1, option   #if option chosen is 1
                     je optionNewContact #then a new contact is inserted
                     jmp endproc            
                     cmpl $2, option  #if option chosen is 2
                     je optionsearchContact #then a contact is searched
                     jmp endproc
optionNewContact:    call askforContactData
                     jmp endproc
optionsearchContact: #call searchContact
                     jmp endproc

endproc:           leave
                   ret


askforContactData: pushl %ebp
                  movl %esp, %ebp
                 #send the appropiate parameters to fopen:
                  pushl $mode1 # mode
                  pushl $file # path: agenda2.dat
                  call fopen # Llama a fopen
                 addl $8, %esp #clean the stack
                 movl %eax, fp # 
                 cmpl $0, fp
                 je errors 
                 #ask contact details
                 pushl $askname
                  call printf
                  addl $4, %esp
                  pushl $name
                  pushl $formatString
                  call scanf
                  addl $8, %esp
                  pushl $asktel
                  call printf
                  addl $4, %esp
                  pushl $tel
                  pushl $formatInteger
                  call scanf #idem
                  addl $8, %esp
                 pushl $askemail
                 call printf
                 addl $4, %esp
                 pushl $email
                 pushl $formatString
                 call scanf
                 addl $8, %esp
                #write in the .txt file
                 pushl $email
                pushl $tel
                pushl $name
                pushl $formatSeparator
                 pushl fp 
                call fprintf
                #close the file
                pushl fp
                call fclose
               addl $4, %esp
              jmp endcontactdata
errors:      pushl $error
             call printf
             jmp endcontactdata          
endcontactdata: leave
                 ret
         
                
searchContact: pushl %ebp
               movl %esp, %ebp
               pushl $mode2
               pushl $file
             leave
ret
